<?php
//dezend by http://www.yunlu99.com/ QQ:270656184
defined('BASEPATH') || exit('No direct script access allowed');
class Fill_run extends MY_Controller
{
	public function __construct()
	{
		parent::__construct();
	}

	public function index()
	{
		set_time_limit(0);
		$this->load->library('bag');
		$coins = $this->d->rows(array('table' => 'coin', 'where' => 'status=1'));

		foreach ($coins as $v) {
			if (!$v['host'] || !$v['username'] || !$v['password'] || !$v['port']) {
				continue;
			}

			$xnbs = $this->d->rows(array('table' => 'xnb', 'where' => 'coin_id=' . $v['id']));

			foreach ($xnbs as $v1) {
				$username = $this->d->field(array('field' => 'username', 'table' => 'user', 'where' => 'id=' . $v1['user_id']));
				$datas = $this->bag->get_transactions($v['host'], $v['username'], $v['password'], $v['port'], $username);

				foreach ($datas as $v2) {
					$rs = array();
					$this->db->query('set autocommit=0');
					$this->db->query('lock tables t_fill_coin write, t_xnb write');

					if (2 <= $v2['confirmations']) {
						$fill = $this->db->where('coin_id', $v1['coin_id'])->where('txid', $v2['txid'])->get('fill_coin')->unbuffered_row('array');

						if ($fill) {
							$this->db->query('rollback');
							$this->db->query('unlock tables');
							continue;
						}

						$rs[] = $this->db->set('num', 'num+' . $v2['amount'], false)->where('user_id', $v1['user_id'])->where('coin_id', $v1['coin_id'])->update('xnb');
						$d = array('user_id' => $v1['user_id'], 'coin_id' => $v1['coin_id'], 'txid' => $v2['txid'], 'num' => $v2['amount'], 'address' => $v2['address'], 'add_time' => $v2['time'], 'status' => 1);
						$rs[] = $this->db->insert('fill_coin', $d);
					}
					else {
						continue;
					}

					if (correct($rs)) {
						$this->db->query('commit');
					}
					else {
						$this->db->query('rollback');
					}

					$this->db->query('unlock tables');
				}
			}
		}
	}
}

?>
